<?php
/******************************************************************************
 *
 *    OpenZoowa - Web App to manage events
 *    Copyright (C) 2008 Jesus Ruiz-Ayucar
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/
   if (!defined('ZOOWA')) {
      include_once('../../config.php');
   }
   require_once(modelspath . 'country.php');

   class Region {
      private $id;
      private $name;
      private $name_clean;
      private $country;

      public function Region($id = null) {
         if (isset($id) && Util::is_numeric($id) && $id > 0) {
            global $db;
            $SQL  = 'SELECT * FROM '.$GLOBALS["db_pre"].'regions WHERE ';
            $SQL .= 'id = "' . $db->escape($id).'"';
            $region = $db->get_row($SQL);
            $this->id = $region->id;
            $this->name = $region->name;
            $this->name_clean = $region->name_clean;
            $this->country = new Country($region->country_id);
         }
      }

      public function setName($name) {
         if (!empty($name) && strlen($name) <= 128) {
            $this->name = $name;
         }
      }

      public function setNameClean($name_clean) {
         if (!empty($name_clean) && strlen($name_clean) <= 128) {
            $this->name_clean = $name_clean;
         }
      }

      public function setCountry($country_id) {
         $country = new Country($country_id);
         $id = $country->getId();
         if (Util::is_numeric($country_id) && isset($country) && isset($id)) {
            $this->country = $country;
         } else {
            $this->country = null;
         }
      }

      public function add() {
         if (!isset($this->id) && !empty($this->name) && !empty($this->name_clean) && isset($this->country)) {
            global $db;
            $SQL = 'INSERT INTO '.$GLOBALS["db_pre"].'regions ';
            $SQL .= '(name, name_clean, country_id) VALUES (';
            $SQL .= '"'.$db->escape($this->name).'",';
            $SQL .= '"'.$db->escape($this->name_clean).'",';
            $SQL .= $db->escape($this->country->getId()).')';
            $db->query($SQL);
            $this->id = $db->insert_id;
            return $this->id;
         }
      }

      public function del() {
         if (isset($this->id)) {
            global $db;

            $SQL  = 'DELETE FROM '.$GLOBALS["db_pre"].'regions WHERE ';
            $SQL .= 'id = '.$db->escape($this->id);
            if($db->query($SQL)) {
               $this->name = null;
               $this->name_clean = null;
               $this->country = null;
               $this->id = null;
            }
         }
      }

      public function getId() {
         return $this->id;
      }

      public function getName() {
         return $this->name;
      }

      public function getNameClean() {
         return $this->name_clean;
      }

      public function getCountry() {
         return $this->country;
      }


/**
 *	HELPERS
 */
      public static function getList($country_id) {
         if (isset($country_id) && Util::is_numeric($country_id)) {
            global $db;
            $SQL = 'SELECT id FROM '.$GLOBALS['db_pre'].'regions ';
            $SQL .= 'WHERE country_id = ' . $db->escape($country_id);
            foreach ($db->get_col($SQL) as $region) {
               $regions[] = new Region($region);
            }

            return $regions;
         }
      }

      public static function getIdFromClean($reg_name, $country_id) {
         global $db;
         $SQL = 'SELECT id FROM '.$GLOBALS["db_pre"].'regions ';
         $SQL .= 'WHERE name_clean = "' . $db->escape($reg_name) .'"';
         $SQL .= 'AND country_id = ' . $db->escape($country_id);
         return $db->get_var($SQL);
      }
   }
?>